package com.chance.cc.crawler.prod.command.job.domain.vm.bitauto.realtime;

import com.alibaba.fastjson.JSON;
import com.chance.cc.crawler.core.CrawlerEnum;
import com.chance.cc.crawler.core.CrawlerJob;
import com.chance.cc.crawler.core.downloader.HttpConfig;
import com.chance.cc.crawler.core.downloader.HttpPage;
import com.chance.cc.crawler.core.filter.FilterInfo;
import com.chance.cc.crawler.core.filter.FilterUtils;
import com.chance.cc.crawler.core.queue.crawler.CrawlerQueueConfig;
import com.chance.cc.crawler.core.record.CrawlerRequestRecord;
import com.chance.cc.crawler.meta.core.bean.CrawlerMetaConstant;
import com.chance.cc.crawler.meta.core.bean.job.CrawlerScheduleJob;
import com.chance.cc.crawler.prod.command.job.domain.vm.bitauto.BitAutoCommonCrawlerSchedulerJob;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

import static com.chance.cc.crawler.core.CrawlerEnum.CrawlerRequestType.*;

/**
 * @author lt
 * @version 1.0
 * @date 2021-01-05 15:51:24
 * @email okprog@sina.com
 */
public class BitAutoWebHaoArticleCrawlerSchedulerJob extends BitAutoCommonCrawlerSchedulerJob {
    private static final String crawler_level = "normal";



    public static void main(String[] args) {
        publishBitAutoCrawlerSchedulerJobInfo();
//        System.out.println(bitAutoScheduler().generateCrawlerTriggerKey());
    }
    public static CrawlerJob publishBitAutoCrawlerSchedulerJobInfo(){
        CrawlerJob bitAutoScheduler = bitAutoScheduler();

        //发布定时任务
        CrawlerScheduleJob crawlerScheduleJob = new CrawlerScheduleJob();
        crawlerScheduleJob.setDomain(domain);
        crawlerScheduleJob.setCrawlerJob(JSON.toJSONString(bitAutoScheduler));
        crawlerScheduleJob.setJobType(CrawlerMetaConstant.ScheduleCrawlerJobType.crawler.enumVal());
        crawlerScheduleJob.setNote("易车号新闻实时采集任务");
        crawlerScheduleJob.setCrawlerKey(bitAutoScheduler.generateCrawlerKey());
        HttpPage httpPage = metaServiceCommand.addOrUpdateCrawlerScheduleJob(crawlerScheduleJob);
        System.out.println("发布任务：" + httpPage.getRawText());
        return bitAutoScheduler;
    }

    public static CrawlerJob bitAutoScheduler(){
        String site="hao_article";
        //易车号-最新文章采集
        CrawlerRequestRecord haoArticleCrawler = doHaoArticleCrawler();
//        String site = haoArticleCrawler.tagsCreator().bizTags().getCategoryTag().getKVTagStrVal("site");
        //request队列名
        String requestQueueName = StringUtils.joinWith("-","crawler",domain, site, crawler_level,"queue");

        //配置结果队列信息
        String resultQueueName = StringUtils.joinWith("-","crawler",domain, site, crawler_level,"result","queue");
        CrawlerQueueConfig crawlerQueueConfig = new CrawlerQueueConfig(
                resultQueueName,
                CrawlerQueueConfig.Content.result,
                CrawlerQueueConfig.Storage.redis,
                CrawlerQueueConfig.Structure.list);

        //同步到mysql callback record
        CrawlerRequestRecord syncCrawlerRecord = CrawlerRequestRecord.builder()
                .startPageRequest("bitauto_hao_article_sync", turnPageItem)
                .httpUrl(metaServiceHttpPrefix + "/crawler/domain/common/api/v1/bitauto/search/results/sync?resultQueue="
                        +resultQueueName + "&site=" + site)
                .requestLabelTag(supportCallback)
                .requestLabelTag(internalDownload)
                .build();

        return CrawlerJob.builder()
                .triggerInfo(domain,
                        CrawlerMetaConstant.ScheduleJobTrigger_Cron,
                        System.currentTimeMillis(),
                        StringUtils.joinWith("-",site,CrawlerMetaConstant.ScheduleJobTriggerJob_Realtime))
                .crawlerRequestQueue(CrawlerMetaConstant.redisRequestQueue(requestQueueName))
                .kafkaResultPipeline("kafka_result",kafkaTopciForRealTimeJob,null)
                .queueResultPipeline("redis_result",crawlerQueueConfig)
                .crawlerJobThreadNumber(25)
                .requestRecord(haoArticleCrawler)
                .supportRecord(syncCrawlerRecord)
                .build();
    }


    public static CrawlerRequestRecord doHaoArticleCrawler(){
        String site = "hao";

        FilterInfo turnPageFilterInfo = new FilterInfo();
        turnPageFilterInfo.setFilter(CrawlerEnum.CrawlerRecordFilter.count);
        turnPageFilterInfo.setCurCount(0);
        turnPageFilterInfo.setCountTotalNum(100);

        String url = "https://hao.yiche.com/site_web/hao/api/get_latest_article_list";
        CrawlerRequestRecord requestRecord = CrawlerRequestRecord.builder()
                .startPageRequest(domain,CrawlerEnum.CrawlerRequestType.turnPage)
                .httpUrl(url)
                .recordKey(url + "1")
                .releaseTime(System.currentTimeMillis())
                .httpConfig(HttpConfig.me(domain))
                .domain(domain)
//                .turnPageFilterInfo(turnPageFilterInfo)
                .filter(CrawlerEnum.CrawlerRecordFilter.keyOrDateRange)
                .addFilterInfo(FilterUtils.redisFilterKeyInfo(domain))
                .httpHead("x-platform","phone")
                .addFilterInfo(FilterUtils.dateRangeFilterInfo(2,null))
                .proxy(proxy)
                .build();
        Map<String,Object> extras = new HashMap<>();
        extras.put("pageIndex",1);
        requestRecord.getHttpRequest().setExtras(extras);
        requestRecord.tagsCreator().bizTags().addDomain(domain);
        requestRecord.tagsCreator().bizTags().addSite(site);//清洗出的数据site=hao_article
        return requestRecord;
    }

}
